/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driving.query.role.repository;

import com.lap.auth.adapter.driving.query.role.RoleQueryConverter;
import com.lap.auth.adapter.driving.query.role.mapper.RoleQueryMapper;
import com.lap.auth.adapter.driving.query.role.po.RoleQueryPo;
import com.lap.auth.application.role.dto.QueryRolePageQuery;
import com.lap.auth.application.role.dto.RoleDto;
import com.lap.auth.application.role.repository.RoleQueryRepository;
import com.lap.framework.common.dto.page.PageResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class RoleQueryRepositoryImpl implements RoleQueryRepository {

  private final RoleQueryConverter roleQueryConverter;
  private final RoleQueryMapper roleQueryMapper;

  @Override
  public PageResponse<RoleDto> findByConditionPaged(QueryRolePageQuery query) {
    long count = roleQueryMapper.countPageByCondition(query);
    if (count == 0L) {
      return PageResponse.ok(count);
    }

    List<RoleQueryPo> list = roleQueryMapper.selectPageByCondition(query);
    return PageResponse.ok(roleQueryConverter.toDtoList(list), count);
  }
}
